﻿package com.teaphy.coroutinecrawler.job

import kotlinx.coroutines.*



fun main() = runBlocking {
    println("start")

    val job = launch {
        println("coroutine1 start -->")
        delay(2000)
//        throw Exception("其他原因执行失败")
        println("coroutine1 --> end ")
    }

    val disposableHandle = job.invokeOnCompletion {
        if (null == it) {
            println("coroutine1 正常执行完成")
            return@invokeOnCompletion
        }

        when (it) {
            is CancellationException ->  println("coroutine1被取消: $it")
            else ->   println("coroutine1因其他原因执行失败")
        }
    }

//    delay(1000)
//
//    job.cancel(CancellationException("手动取消"))

    println("end")
}